package com.sentiment.database.codec;

import static java.util.Arrays.asList;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bson.BsonBinarySubType;
import org.bson.BsonDocument;
import org.bson.BsonDocumentWriter;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonValue;
import org.bson.BsonWriter;
import org.bson.Transformer;
import org.bson.assertions.Assertions;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.Codec;
import org.bson.codecs.CollectibleCodec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.IdGenerator;
import org.bson.codecs.ObjectIdGenerator;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistry;

import com.sentiment.database.bean.HotspotReport;

public class HotspotReportCodec implements CollectibleCodec<HotspotReport>{
	 private static final String ID_FIELD_NAME = "_id";
	    private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(asList(new ValueCodecProvider(),
	            new BsonValueCodecProvider(),
	            new DocumentCodecProvider()));
	    private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();

	    private final CodecRegistry registry;
	    private final BsonTypeClassMap bsonTypeClassMap;
	    private final IdGenerator idGenerator;
	    private final Transformer valueTransformer;

	    public HotspotReportCodec() {
	        this(DEFAULT_REGISTRY, DEFAULT_BSON_TYPE_CLASS_MAP);
	    }

	    public HotspotReportCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap) {
	        this(registry, bsonTypeClassMap, null);
	    }

	    public HotspotReportCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
	        this.registry = Assertions.notNull("registry", registry);
	        this.bsonTypeClassMap = Assertions.notNull("bsonTypeClassMap", bsonTypeClassMap);
	        this.idGenerator = Assertions.notNull("idGenerator", new ObjectIdGenerator());
	        this.valueTransformer = valueTransformer != null ? valueTransformer : new Transformer() {
	            @Override
	            public Object transform(final Object value) {
	                return value;
	            }
	        };
	    }
	    @Override
	    public boolean documentHasId(final HotspotReport document) {
	        return document.containsKey(ID_FIELD_NAME);
	    }

	    @Override
	    public BsonValue getDocumentId(final HotspotReport document) {
	        if (!documentHasId(document)) {
	            throw new IllegalStateException("The document does not contain an _id");
	        }

	        Object id = document.get(ID_FIELD_NAME);
	        if (id instanceof BsonValue) {
	            return (BsonValue) id;
	        }

	        BsonDocument idHoldingDocument = new BsonDocument();
	        BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
	        writer.writeStartDocument();
	        writer.writeName(ID_FIELD_NAME);
	        writeValue(writer, EncoderContext.builder().build(), id);
	        writer.writeEndDocument();
	        return idHoldingDocument.get(ID_FIELD_NAME);
	    }

	    @Override
	    public HotspotReport generateIdIfAbsentFromDocument(final HotspotReport document) {
	        if (!documentHasId(document)) {
	            document.put(ID_FIELD_NAME, idGenerator.generate());
	        }
	        return document;
	    }

	    @Override
	    public void encode(final BsonWriter writer, final HotspotReport document, final EncoderContext encoderContext) {
//	        LOG.info("encoderContext.isEncodingCollectibleDocument() : "+ encoderContext.isEncodingCollectibleDocument());
	        writeMap(writer, document, encoderContext);
	    }

	    @Override
	    public HotspotReport decode(final BsonReader reader, final DecoderContext decoderContext) {

	    	HotspotReport document = new HotspotReport();

	        reader.readStartDocument();
	        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
	            String fieldName = reader.readName();
	            document.put(fieldName, readValue(reader, decoderContext));
	        }

	        reader.readEndDocument();

	        return document;
	    }

	    @Override
	    public Class<HotspotReport> getEncoderClass() {
	        return HotspotReport.class;
	    }

	    private void beforeFields(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Map<String, Object> document) {
	        if (encoderContext.isEncodingCollectibleDocument() && document.containsKey(ID_FIELD_NAME)) {
	            bsonWriter.writeName(ID_FIELD_NAME);
	            writeValue(bsonWriter, encoderContext, document.get(ID_FIELD_NAME));
	        }
	    }

	    private boolean skipField(final EncoderContext encoderContext, final String key) {
	        return encoderContext.isEncodingCollectibleDocument() && key.equals(ID_FIELD_NAME);
	    }

	    @SuppressWarnings({"unchecked", "rawtypes"})
	    private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) {
	        if (value == null) {
	            writer.writeNull();
	        } else if (Iterable.class.isAssignableFrom(value.getClass())) {
	            writeIterable(writer, (Iterable<Object>) value, encoderContext.getChildContext());
	        } else if (Map.class.isAssignableFrom(value.getClass())) {
	            writeMap(writer, (Map<String, Object>) value, encoderContext.getChildContext());
	        } else {
	            Codec codec = registry.get(value.getClass());
	            encoderContext.encodeWithChildContext(codec, writer, value);
	        }
	    }

	    private void writeMap(final BsonWriter writer, final Map<String, Object> map, final EncoderContext encoderContext) {
	        writer.writeStartDocument();

	        beforeFields(writer, encoderContext, map);

	        for (final Map.Entry<String, Object> entry : map.entrySet()) {
	            if (skipField(encoderContext, entry.getKey())) {
	                continue;
	            }
	            writer.writeName(entry.getKey());
	            writeValue(writer, encoderContext, entry.getValue());
	        }
	        writer.writeEndDocument();
	    }

	    private void writeIterable(final BsonWriter writer, final Iterable<Object> list, final EncoderContext encoderContext) {
	        writer.writeStartArray();
	        for (final Object value : list) {
	            writeValue(writer, encoderContext, value);
	        }
	        writer.writeEndArray();
	    }

	    private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
	        BsonType bsonType = reader.getCurrentBsonType();
	        if (bsonType == BsonType.NULL) {
	            reader.readNull();
	            return null;
	        } else if (bsonType == BsonType.ARRAY) {
	           return readList(reader, decoderContext);
	        } else if (bsonType == BsonType.BINARY) {
	            byte bsonSubType = reader.peekBinarySubType();
	            if (bsonSubType == BsonBinarySubType.UUID_STANDARD.getValue() || bsonSubType == BsonBinarySubType.UUID_LEGACY.getValue()) {
	                return registry.get(UUID.class).decode(reader, decoderContext);
	            }
	        }
	        return valueTransformer.transform(registry.get(bsonTypeClassMap.get(bsonType)).decode(reader, decoderContext));
	    }

	    private List<Object> readList(final BsonReader reader, final DecoderContext decoderContext) {
	        reader.readStartArray();
	        List<Object> list = new ArrayList<Object>();
	        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
	            list.add(readValue(reader, decoderContext));
	        }
	        reader.readEndArray();
	        return list;
	    }
}
